﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using System.Data;
using Comm;
using BusinessFacade;
using Entities;

namespace MedicareNoteManagerWeb.Web
{
    public partial class Notemessage : BasePage
    {
        nm_notemessage_Model model1 = new nm_notemessage_Model();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                model1 = (nm_notemessage_Model)Session["NotemessageModel"];
                BindDrop();
                PageGrid1_Invalidate(sender, e);
                PageGrid1_SelectedValueChanged(sender, e);
                lbl_Message.Text = "";
            }
        }
        public void BindDrop()
        {
            FillDropDownList("GetDrop", "AreaID", "AreaName", "请选择区域", this.FindDropArea);
            FillDropDownList("GetDrop", "AreaID", "AreaName", "请选择区域", this.DropArea);
        }
        protected void PageGrid1_SelectedValueChanged(object sender, EventArgs e)
        {
            Busi_nm_notemessage u = new  Busi_nm_notemessage();
            nm_notemessage_Model model = u.GetListModel(int.Parse(PageGrid1.SelectedValue));
            txt_Name.Text = model.MoubleName;
            txt_Content.Value = model.MoubleContent;
            txt_Explain.Value = model.MoubleExplain;
            DropArea.SelectedValue = model.MoubleArea.ToString();
        }

        protected void PageGrid1_Invalidate(object sender, EventArgs e)
        {
            Busi_nm_notemessage users = new Busi_nm_notemessage();
            DataTable dt = users.GetList(txt_FindUser.Text.Trim(), int.Parse(FindDropArea.SelectedValue));
            PageGrid1.BindFullDataTable(dt, true);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                PageGrid1_Invalidate(sender, e);
                PageGrid1_SelectedValueChanged(sender, e);
                lbl_Message.Text = "";
            }
            catch (Exception ex)
            {
                lbl_Message.Text = ex.Message.ToString();
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            PageGrid1.ResetState();
            txt_Content.Value = "";
            txt_Explain.Value = "";
            txt_Name.Text = "";
            DropArea.SelectedValue = "0";
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button3_Click(object sender, EventArgs e)
        {
            try
            {
                int id = int.Parse(PageGrid1.SelectedValue);
                Busi_nm_notemessage.Delete(id);
                PageGrid1_Invalidate(sender, e);
                PageGrid1_SelectedValueChanged(sender, e);
                MessageBox.Show(this.Page, "删除成功！");
            }
            catch (Exception ex)
            {
                lbl_Message0.Text = ex.Message;
            }

        }
        public string CheckData()
        {
            string strErr = "";
            if(string.IsNullOrEmpty(txt_Name.Text.Trim()))
            {
                strErr += "模板名称不能为空";
            }
           
            if(DropArea.SelectedValue=="0")
            {
                strErr += "请选择区域";
            }
            return strErr;
        }


        //添加
        protected void Button4_Click(object sender, EventArgs e)
        {
            model1 = (nm_notemessage_Model)Session["NotemessageModel"];
            string strErr = CheckData();
            if (string.IsNullOrEmpty(strErr))
            {
                if (this.PageGrid1.SelectedValue == null)
                {
                    nm_notemessage_Model model = new nm_notemessage_Model();
                    model.MoubleArea = int.Parse(DropArea.SelectedValue).ToString();
                    model.MoubleName = txt_Name.Text;
                    model.MoubleContent = txt_Content.Value;
                    model.MoubleExplain = txt_Explain.Value;
                    if (new Busi_nm_notemessage().Add(model))
                    {
                        MessageBox.Show(this.Page, "添加成功！");
                        PageGrid1_Invalidate(sender, e);
                    }
                    else
                    {
                        MessageBox.Show(this.Page, "添加失败！");
                    }
                }
                else
                {
                    try
                    {
                        //修改
                        nm_notemessage_Model model = new nm_notemessage_Model();
                        model.MoubleArea = int.Parse(DropArea.SelectedValue).ToString();
                        model.MoubleName = txt_Name.Text;
                        model.MoubleContent = txt_Content.Value;
                        model.MoubleExplain = txt_Explain.Value;
                        model.ID = int.Parse(PageGrid1.SelectedValue);
                        new Busi_nm_notemessage().Update(model);
                        MessageBox.Show(this.Page, "OK！");
                        PageGrid1_Invalidate(sender, e);
                    }
                    catch (System.Exception ex)
                    {
                        lbl_Message1.Text = ex.Message;
                    }
                }
            }
            else
            {
                lbl_Message1.Text = strErr;
            }
        }
    }
}